﻿using System;
using System.Collections.Generic;
using System.Text;

namespace FiniteStateMachine
{
    public class FsmTimerEvent:FsmEvent
    {
        #region members
        private DateTime _ExpiryTime;
        /// <summary></summary>
        public DateTime ExpiryTime
        {
            get
            {
                return this._ExpiryTime;
            }
            set
            {
                this._ExpiryTime = value;
            }
        }

        private bool _Enabled=true;
        /// <summary></summary>
        public bool Enabled
        {
            get
            {
                return this._Enabled;
            }
            set
            {
                this._Enabled = value;
            }
        }

        private int _NofRepeats;
        /// <summary></summary>
        public int Repeats
        {
            get
            {
                return this._NofRepeats;
            }
            set
            {
                this._NofRepeats = value;
            }
        }

        private TimeSpan _DelayTime;
        /// <summary></summary>
        public TimeSpan DelayTime
        {
            get
            {
                return this._DelayTime;
            }
        }

        public TimeSpan ExpiryDelay
        {
            get
            {
                return this._ExpiryTime.Subtract(DateTime.Now);
            }
        }
        #endregion

        public FsmTimerEvent(DateTime expiryTime)
        {
            this.ExpiryTime = expiryTime;
        }

        public FsmTimerEvent(TimeSpan delay)
        {
            this.ExpiryTime = DateTime.Now + delay;
            this._DelayTime = delay;
            this.Repeats = 0;
        }

        public FsmTimerEvent(TimeSpan delay, int nRepeats)
        {
            this.ExpiryTime = DateTime.Now + delay;
            this._DelayTime = delay;
            this.Repeats = nRepeats;
            if(nRepeats<int.MaxValue)
            {
                this.Repeats -= 1;
            }
        }

        internal void DecRepeats()
        {
            this.ExpiryTime += this.DelayTime;
            if(this.Repeats >0 && this.Repeats<int.MaxValue)
            {
                this.Repeats--;
            }
        }
    }
}
